/* ----------------------------------------------------------------------------
 * Copyright (c) 2008 by Airbus SAS. All rights reserved. All intellectual
 * property rights in and/or in the computer program and its related
 * documentation and technology are the sole Airbus' property. This computer
 * program is under Airbus copyright and cannot be in whole or in part
 * reproduced, sublicensed, leased, sold or used in any form or by any means,
 * including without limitation graphic, electronic, mechanical, photocopying,
 * recording, taping or information storage and retrieval systems without Airbus
 * prior written consent. The downloading, exporting or reexporting of this
 * computer program or any related documentation or technology is subject to any
 * export rules, including US regulations.
 * ----------------------------------------------------------------------------
 */

package com.azzaou.util;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

/**
 * BuildMessage
 * 
 * @version : V1.0
 * @author : moabdi
 * @Project : [AWN_PACAT_ENGINEERING_ADMINTOOLS]
 * @Date : mai. 2012
 * @DSL_Requirements :
 * @Product_Requirements :
 */
public class BuildMessage 
{
  /**
   * Logger for this class
   */
  private static final Logger LOGGER = Logger.getLogger(BuildMessage.class);

  /**
   * add Message
   * 
   * @param id
   *          : id
   * @param summary
   *          : summary
   * @param detail
   *          : detail
   * @param severity
   *          : severity
   */
	public static void addMessage(String id, String summary, String detail,
		FacesMessage.Severity severity)
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage();
		facesMessage.setSeverity(severity);
		facesMessage.setSummary(summary);
		facesMessage.setDetail(detail);
		facesContext.addMessage(id, facesMessage);
	}
	
	/**
	 * add Message From Bundle
	 * @param id : id
	 * @param summary : summary
	 * @param detail : detail
	 * @param severity  :severity
	 * @param params  :params
	 */
	public static void addMessageFromBundle(String id, String summary,
	    String detail,
		FacesMessage.Severity severity, Object params[])
	{

		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage();
		String summaryText = getMessageResourceString(facesContext.getApplication()
				.getMessageBundle(), summary, params, facesContext.getViewRoot()
				.getLocale());
		String detailText = getMessageResourceString(facesContext.getApplication()
				.getMessageBundle(), detail, params, facesContext.getViewRoot()
				.getLocale());
		
		facesMessage.setSeverity(severity);
		facesMessage.setSummary(summaryText);
		facesMessage.setDetail(detailText);
		facesContext.addMessage(id, facesMessage);

	}

  /**
   * get Current Class Loader
   * 
   * @param defaultObject
   *          : defaultObject
   * @return ClassLoader
   */
	protected static ClassLoader getCurrentClassLoader(Object defaultObject)
	{
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		
		if(loader == null)
		{
			loader = defaultObject.getClass().getClassLoader();
		}
		return loader;
	}

  /**
   * get Message Resource String
   * 
   * @param bundleName
   *          : bundleName
   * @param key
   *          : key
   * @param params
   *          :params
   * @param locale
   *          : locale
   * @return String
   */
	public static String getMessageResourceString(
			String bundleName, String key, 
			Object params[], Locale locale)
	{
		String text = null;
		ResourceBundle bundle = 
				ResourceBundle.getBundle(bundleName, locale, 
										getCurrentClassLoader(params));
		try
		{
			text = bundle.getString(key);
		} 
		catch(MissingResourceException e)
		{
      LOGGER.error(
          "An error occured : getMessageResourceString :  ", e);
		}
		return text;
	}
}
